﻿using Marbles.Core.Content.Profiles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.Web.Admin.Content
{
    public partial class ProfileQuestions : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            ProfileQuestionCollection q = new ProfileQuestionCollection();
            cGrdProfile.DataSource = q;
            cGrdProfile.DataBind();

        }

        private void LoadLookups()
        {
            cDrpQuestionType.Items.Clear();
            cDrpQuestionType.DataSource = ProfileQuestionCollection.GetProfileQuestionTypes();
            cDrpQuestionType.DataBind();
            cDrpQuestionType.Items.Insert(0, new ListItem("", "0"));
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            ProfileQuestionCollection items = new ProfileQuestionCollection();

            switch (e.CommandName)
            {
                case "New":
                    cTxtDescription.Text = "";
                    cDrpQuestionType.ClearSelection();
                    LoadLookups();
                    cBtnSave.CommandName = "Insert";
                    cBtnSave.CommandArgument = e.CommandArgument.ToString();
                    cDlgProfile.Visible = true;
                    cBtnDelete.Visible = false;
                    break;
                case "Save":
                    foreach (ProfileQuestion p in items)
                    {
                        if (p.QuestionId == Convert.ToInt32(e.CommandArgument.ToString()))
                        {
                            p.Description = cTxtDescription.Text.Trim();
                            p.QuestionTypeValue = Convert.ToInt32(cDrpQuestionType.SelectedValue);
                            p.Update();
                            break;
                        }
                    }
                    cBtnSave.CommandName = "";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cDlgProfile.Visible = false;
                    LoadData();
                    break;
                case "Cancel":
                    cBtnSave.CommandName = "";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cDlgProfile.Visible = false;
                    break;
                case "Modify":
                    cDlgProfile.Title = "Modify Profile Question";
                    LoadLookups();

                    foreach (ProfileQuestion p in items)
                    {
                        if (p.QuestionId == Convert.ToInt32(e.CommandArgument.ToString()))
                        {
                            cTxtDescription.Text = p.Description;
                            cDrpQuestionType.ClearSelection();
                            cDrpQuestionType.SelectedValue = p.QuestionTypeValue.ToString();
                            break;
                        }
                    }
                    cDlgProfile.Visible = true;
                    cBtnSave.CommandName = "Save";
                    cBtnSave.CommandArgument = e.CommandArgument.ToString();
                    cBtnDelete.CommandArgument = e.CommandArgument.ToString();
                    cBtnDelete.Visible = true;
                    break;
                case "Insert":
                    cDlgProfile.Title = "Add Profile Question";
                    ProfileQuestion item = ProfileQuestion.CreateNew(items.SiteId);
                    item.Description = cTxtDescription.Text.Trim();
                    item.QuestionTypeValue = Convert.ToInt32(cDrpQuestionType.SelectedValue);
                    item.Insert();
                    cBtnSave.CommandName = "";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cDlgProfile.Visible = false;
                    LoadData();
                    break;
                case "Delete":
                    foreach (ProfileQuestion p in items)
                    {
                        if (p.QuestionId == Convert.ToInt32(e.CommandArgument.ToString()))
                        {
                            p.Delete();
                            break;
                        }
                    }
                    cBtnSave.CommandName = "";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cDlgProfile.Visible = false;
                    LoadData();
                    break;
            }
        }
    }
}